import { BASE_URL, TIMEOUT } from "../config/config";
import axios from "axios";
import type { AxiosRequestConfig } from "axios";
import { refreshToken } from "./user/user";
import { message } from "antd";

// import dayjs from "dayjs";
// import { RegisterUser } from "../page/register/Register";
// import { UpdatePassword } from "../page/update_password/UpdatePassword";
// import { UserInfo } from "../page/update_info/UpdateInfo";
// import { SearchBooking } from "../page/booking_history/BookingHistory";
// import { CreateBooking } from "../page/meeting_room_list/CreateBookingModal";

const axiosInstance = axios.create({
  baseURL: BASE_URL,
  timeout: TIMEOUT,
});

axiosInstance.interceptors.request.use(function (config) {
  const accessToken = localStorage.getItem("access_token");
  if (accessToken) {
    config.headers.authorization = "Bearer " + accessToken;
  }
  return config;
});

axiosInstance.interceptors.response.use(
  (response) => {
    return response.data || response;
  },
  async (error) => {
    const { data, config } = error.response;
    if (data.code === 401 && !config.url.includes("/user/refresh")) {
      const res = await refreshToken();
      if (res.status === 200) {
        return axios(error.response.config);
      } else {
        message.error(res.data);
        return setTimeout(() => {
          window.location.href = "/login";
        }, 1500);
      }
    } else if (data.code === 400) {
      message.error(data.data);
    }
    return Promise.reject(data.data);
  }
);

// axios封装
export async function ajaxService<T = any>(config: AxiosRequestConfig) {
  try {
    const res: T = await axiosInstance(config);
    return Promise.resolve(res);
  } catch (err: any) {
    return Promise.reject(err);
  }
}

// export async function registerCaptcha(email: string) {
//   return await axiosInstance.get("/user/register-captcha", {
//     params: {
//       address: email,
//     },
//   });
// }

// export async function register(registerUser: RegisterUser) {
//   return await axiosInstance.post("/user/register", registerUser);
// }

// export async function updatePasswordCaptcha(email: string) {
//   return await axiosInstance.get("/user/update_password/captcha", {
//     params: {
//       address: email,
//     },
//   });
// }

// export async function updatePassword(data: UpdatePassword) {
//   return await axiosInstance.post("/user/update_password", data);
// }

// export async function getUserInfo() {
//   return await axiosInstance.get("/user/info");
// }

// export async function updateInfo(data: UserInfo) {
//   return await axiosInstance.post("/user/update", data);
// }

// export async function updateUserInfoCaptcha() {
//   return await axiosInstance.get("/user/update/captcha");
// }

// export async function searchMeetingRoomList(
//   name: string,
//   capacity: number,
//   equipment: string,
//   pageNo: number,
//   pageSize: number
// ) {
//   return await axiosInstance.get("/meeting-room/list", {
//     params: {
//       name,
//       capacity,
//       equipment,
//       pageNo,
//       pageSize,
//     },
//   });
// }

// export async function bookingList(
//   searchBooking: SearchBooking,
//   pageNo: number,
//   pageSize: number
// ) {
//   let bookingTimeRangeStart;
//   let bookingTimeRangeEnd;

//   if (searchBooking.rangeStartDate && searchBooking.rangeStartTime) {
//     const rangeStartDateStr = dayjs(searchBooking.rangeStartDate).format(
//       "YYYY-MM-DD"
//     );
//     const rangeStartTimeStr = dayjs(searchBooking.rangeStartTime).format(
//       "HH:mm"
//     );
//     bookingTimeRangeStart = dayjs(
//       rangeStartDateStr + " " + rangeStartTimeStr
//     ).valueOf();
//   }

//   if (searchBooking.rangeEndDate && searchBooking.rangeEndTime) {
//     const rangeEndDateStr = dayjs(searchBooking.rangeEndDate).format(
//       "YYYY-MM-DD"
//     );
//     const rangeEndTimeStr = dayjs(searchBooking.rangeEndTime).format("HH:mm");
//     bookingTimeRangeEnd = dayjs(
//       rangeEndDateStr + " " + rangeEndTimeStr
//     ).valueOf();
//   }

//   return await axiosInstance.get("/booking/list", {
//     params: {
//       username: searchBooking.username,
//       meetingRoomName: searchBooking.meetingRoomName,
//       meetingRoomPosition: searchBooking.meetingRoomPosition,
//       bookingTimeRangeStart,
//       bookingTimeRangeEnd,
//       pageNo: pageNo,
//       pageSize: pageSize,
//     },
//   });
// }

// export async function unbind(id: number) {
//   return await axiosInstance.get("/booking/unbind/" + id);
// }

// export async function bookingAdd(booking: CreateBooking) {
//   const rangeStartDateStr = dayjs(booking.rangeStartDate).format("YYYY-MM-DD");
//   const rangeStartTimeStr = dayjs(booking.rangeStartTime).format("HH:mm");
//   const startTime = dayjs(
//     rangeStartDateStr + " " + rangeStartTimeStr
//   ).valueOf();

//   const rangeEndDateStr = dayjs(booking.rangeEndDate).format("YYYY-MM-DD");
//   const rangeEndTimeStr = dayjs(booking.rangeEndTime).format("HH:mm");
//   const endTime = dayjs(rangeEndDateStr + " " + rangeEndTimeStr).valueOf();

//   return await axiosInstance.post("/booking/add", {
//     meetingRoomId: booking.meetingRoomId,
//     startTime,
//     endTime,
//     note: booking.note,
//   });
// }
